package com.dms.service.impl;

import com.dms.mapper.BuildingManageMapper;
import com.dms.pojo.Building;
import com.dms.service.BuildingManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @className: BuildingManageServiceImpl
 * @package: com.dms.service.impl
 * @description 楼宇管理模块业务逻辑实现层
 * @author: sk
 */
@Service
public class BuildingManageServiceImpl implements BuildingManageService {
    @Autowired
    BuildingManageMapper buildingManageMapper;

    /**
     * 获取楼宇列表数据
     * @param query
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public List<Building> getBuildingLimitList(String query, int select, int pageNum, int pageSize) {
        Map<String,Object> map = new HashMap<String, Object>();
        //计算分页大小
        int start = (pageNum - 1) * pageSize;

        //获取楼栋列表的所有id   解决一个楼栋有多个宿管时，pageSize将其算进其中
        List<Building> buildingIdList = getBuildingIdList();
        //循环遍历id,count超过1，则pageSize相应进行改变 start同上
        int num = 0;
        for (Building building : buildingIdList) {
            int count = buildingManageMapper.getTheSameManageBuildingCount(building.getBuildingId());
            num += count > 1 ? count - 1 : 0;
            pageSize += count > 1 ? count - 1 : 0;
        }

        if (select == 2) { //根据男女性别查询楼栋
            query = String.valueOf(query.contains("男") ? 1 : query.contains("女") ? 0 : "");
        }
        map.put("query", query);
        map.put("select", select);
        map.put("start", start != 0 ? start + num : start); //如果不是第一页，则start要加上前面num的值
        map.put("size", pageSize);
        return buildingManageMapper.getBuildingLimitList(map);
    }

    /**
     * 获取楼栋列表所有信息
     * @return
     */
    @Override
    public List<Building> getBuildingIdList(){
        return buildingManageMapper.getBuildingIdList();
    }

    /**
     * 获取楼栋列表total
     * @param query
     * @param select
     * @return
     */
    @Override
    public int getBuildingTotal(String query, int select) {
        Map<String,Object> map = new HashMap<String, Object>();
        if (select == 2) { //根据男女性别查询楼栋
            query = String.valueOf(query.contains("男") ? 1 : query.contains("女") ? 0 : "");
        }
        map.put("query", query);
        map.put("select", select);
        return buildingManageMapper.getBuildingTotal(map);
    }

    /**
     * 判断楼栋号是否存在
     * @param buildingNumber
     * @return
     */
    @Override
    public boolean checkBuildingNumber(String buildingNumber){
        return buildingManageMapper.checkBuildingNumber(buildingNumber) != null ? true : false;
    }

    /**
     * 添加新的楼栋
     * @param building
     * @return
     */
    @Override
    public int addBuilding(Building building){
        return buildingManageMapper.addBuilding(building);
    }


    /**
     * 根据 楼栋号查询楼栋信息
     * @param buildingNumber
     * @return
     */
    @Override
    public Building getBuildingByNumber(String buildingNumber){
        return buildingManageMapper.getBuildingByNumber(buildingNumber);
    }

    /**
     * 根据 楼栋号 修改楼栋信息
     * @param building
     * @return
     */
    @Override
    public int editBuildingByNumber(Building building) {
        return buildingManageMapper.editBuildingByNumber(building);
    }

    /**
     * 删除楼栋信息
     * @param buildingId
     * @return
     */
    @Override
    public int deleteBuilding(int buildingId) {
        return buildingManageMapper.deleteBuilding(buildingId);
    }
}
